﻿using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Comunication;

namespace ServerDomain
{
    class TcpServerPacketProcessor : ITcpPacketProcessor
    {
        private byte[] _currentBuffer = new byte[0];
        public Socket SocketConnection;
        internal int Bandwith;
        internal int paqueteActual;
        internal int totalPaquetes;
        public AutoResetEvent EventAutoReset;

        #region Implementation of ITcpPacketProcessor

        public void ProcessPacket(byte[] buffer, Socket handler)
        {
            SocketConnection = handler;
            Bandwith = buffer.Length;
            RecursivePacket(buffer, handler);
        }

        public void EndConnection(Socket handler)
        {
            EventAutoReset.Set();
        }

        internal void RecursivePacket(byte[] buffer, Socket handler)
        {
            if (buffer.Length + _currentBuffer.Length < 10)
            {
                IEnumerable<byte> bytes = _currentBuffer.Concat(buffer);
                _currentBuffer = bytes.ToArray();
            }
            else
            {
                IEnumerable<byte> bytes = _currentBuffer.Concat(buffer);
                byte[] tempArray = bytes.ToArray();

                UTF8Encoding decoder = new UTF8Encoding();
                int dataLength = int.Parse(decoder.GetString(tempArray, 5, 5));
                if (tempArray.Length < (dataLength + 10))
                {
                    _currentBuffer = tempArray;
                }
                else if (tempArray.Length >= (dataLength + 10))
                {
                    byte[] dataInfo = tempArray.Take(dataLength + 10).ToArray();
                    ProcessComand(dataInfo, handler);
                    _currentBuffer = new byte[0];
                    if (tempArray.Length > (dataLength + 10) && ((byte)tempArray.GetValue(dataLength + 10)) != 0)
                    {
                        RecursivePacket(tempArray.Skip(dataLength + 10).ToArray(), handler);
                    }
                }
            }
        }

        private void ProcessComand(byte[] bufferToProcess, Socket handler)
        {
            UTF8Encoding decoder = new UTF8Encoding();
            string header = decoder.GetString(bufferToProcess, 0, 3);
            string cmd = decoder.GetString(bufferToProcess, 3, 2);

            Commands command = (Commands)int.Parse(cmd);
            switch (command)
            {
                case Commands.SERVER_SERVER_SEND_INFORMATION_RESPONSE:
                    if (header.Equals("RES"))
                    {
                        EventAutoReset.Set();
                    }
                    break;
                case Commands.SERVER_SERVER_SEND_FILE_NEXT_PACKAGE:
                    if (header.Equals("REQ"))
                    {
                        EventAutoReset.Set();
                    }
                    break;
                case Commands.SERVER_SERVER_SEND_FILE_RECEIVED:
                    if (header.Equals("RES"))
                    {
                        EventAutoReset.Set();
                    }
                    break;
            }

        }

        #endregion
    }
}
